package com.cgsrp.cgsrp_baby.controller;

import com.cgsrp.cgsrp_baby.domain.GrowthRecord;
import com.cgsrp.cgsrp_baby.service.GrowthRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.List;

@RestController
@RequestMapping("growth")
public class GrowthRecordController {
    @Autowired
    private GrowthRecordService recordService;

    @GetMapping("list")
    public List<GrowthRecord> list(int id){
        return recordService.list(id);
    }

    @PostMapping("add")
    public boolean add(GrowthRecord growthRecord){
        growthRecord.setWeight(Math.round(growthRecord.getWeight() * 100.0) / 100.0);
        growthRecord.setHeight(Math.round(growthRecord.getHeight() * 100.0) / 100.0);
        growthRecord.setHeadCircumference(Math.round(growthRecord.getHeadCircumference() * 100.0) / 100.0);
        return recordService.insert(growthRecord)>0;
    }

    @PostMapping("update")
    public boolean update(GrowthRecord growthRecord){
        growthRecord.setWeight(Math.round(growthRecord.getWeight() * 100.0) / 100.0);
        growthRecord.setHeight(Math.round(growthRecord.getHeight() * 100.0) / 100.0);
        growthRecord.setHeadCircumference(Math.round(growthRecord.getHeadCircumference() * 100.0) / 100.0);
        return recordService.updateByPrimaryKey(growthRecord)>0;
    }

    @PostMapping("del")
    public boolean del(int id){
        return recordService.deleteByPrimaryKey(id)>0;
    }
}
